package com.example.imtoken.modules.main.wallet.manage.admin.pass_tips

import androidx.lifecycle.MutableLiveData
import com.example.imtoken.core.base.BaseViewModel
import com.example.imtoken.room.base.AppDataBase
import com.example.imtoken.room.schema.AccountSchema

class PassWordTipsViewModel : BaseViewModel() {

    val passTips = MutableLiveData<String>()

    val isSuccess = MutableLiveData(false)

    var _passTips: String? = null
        set(value) {
            field = value
            inputVerify()
        }

    private var account: AccountSchema? = null

    fun getDefAccount() {
        val appLogin = AppDataBase.instance.getAppLoginDao().getAppLogin()
        val account = AppDataBase.instance.getAccountDao().getDefAccount(appLogin.walletAddress)
        this.account = account
        passTips.value = account.prompt
    }

    fun getDefAccount(key: String?) {
        val account = AppDataBase.instance.getAccountDao().getDefAccount(key)
        this.account = account
        passTips.value = account.prompt
    }

    private fun inputVerify() {
        isSuccess.value = _passTips?.length ?: 0 > 0 && _passTips != account?.prompt
    }

    fun savePassTips(): Boolean {
        account?.let {
            it.prompt = passTips.value
            return AppDataBase.instance.getAccountDao().updateAccount(it) > 0
        }
        return false
    }

}